package com.jiangcheng.demo.service.impl;

import com.jiangcheng.demo.entity.TreeHole;
import com.jiangcheng.demo.mapper.TreeHoleMapper;
import com.jiangcheng.demo.service.TreeHoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangcheng.demo.utils.NewResult;
import com.jiangcheng.demo.utils.TreeHoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-08-09
 */
@Service
public class TreeHoleServiceImpl extends ServiceImpl<TreeHoleMapper, TreeHole> implements TreeHoleService {
    @Resource
    TreeHoleMapper treeHoleMapper;
    @Autowired
    CommentServiceImpl commentService;
    @Autowired
    ReplyServiceImpl replyService;

    @Override
    public NewResult insert(TreeHole treeHole) {
        int insert = treeHoleMapper.insert(treeHole);
        if (insert != 1){
            return new NewResult(400,false, "发布失败");
        }
        return new NewResult(200,true, "发布成功");
    }

    @Override
    public NewResult findTreeHoleByPage(Integer page, Integer pageSize, Integer uId) {
        List<TreeHoleVo> treeHoleVos = treeHoleMapper.findArTreeHoleByPage((page - 1) * pageSize, pageSize, uId);

        if (treeHoleVos.size() <= 0) {
            return new NewResult(400, false, "暂无更多");
        }

        return new NewResult(200, true, "发现" + treeHoleVos.size() + "条",treeHoleVos);
    }

    @Override
    public NewResult countByUid(Integer uId) {
        Integer count = treeHoleMapper.countByUid(uId);
        if (count <= 0){
            return new NewResult(400,false,"还未创建树洞");
        }


        return new NewResult(200,true,"获取成功",count);
    }

    @Override
    public NewResult findTreeHoleByUid(Integer uId) {
        List<TreeHole> treeHoles = treeHoleMapper.findTreeHoleByUid(uId);
        if (treeHoles.size() <= 0){
            return new NewResult(400,false,"获取个人树洞失败");
        }


        return new NewResult(200,true,"获取个人树洞成功", treeHoles);
    }

    @Override
    public NewResult findCommentByTId(Integer tId) {

        return commentService.findCommentsByTreeId(tId);
    }

    /**
     * 自动注入回复实现类 并调用其方法
     * @param cId
     * @return NewResult
     */
    @Override
    public NewResult findReplyByCommentId(Integer cId,Integer uId) {
        return replyService.findReplyByCommentId(cId,uId);
    }
}
